home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14542 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: solon.com!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: fflush(stdin) - not guaranteed to work?
  5. Date: 15 Apr 1996 13:12:29 -0500
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4ku3id$53h@solutions.solon.com>
  10. References: <4ksjpn$rjt@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4ksjpn$rjt@solutions.solon.com>,
  14. Himanshu Gohel <gohel@csee.usf.edu> wrote:
  15. >In Paul S. Wang's "Introduction to ANSI C on UNIX" he says that
  16. >the fflush() function was "not intended to control input buffering"
  17. >on page 266.
  18. >
  19. >My question is, why?  Some of my students have written programs using
  20. >fflush(stdin) hoping it would clear away anything that's in the input
  21. >buffer before a scanf() statement and apparently it works on some PC
  22. >based compilers, but when re-compiled on a UNIX system, it does not
  23. >always work.
  24.  
  25. There is a detailed discussion in the comp.lang.c FAQ that covers VMS, UNIX and
  26. DOS in this respect, as far as I recall. It may be downloaded via FTP
  27. from rtfm.mit.edu/put/usenet
  28.  
  29. Questions about keyboard buffers are very frequent on this newsgroup.
  30.  
  31. The C language has no concept of a keyboard buffer. The standard IO library
  32. functions have their own buffers. But these are different entities from
  33. hardware keyboard buffers, or buffers internal to the operating system. The
  34. contents of the latter two are inaccessible from the C language.
  35.  
  36. There may be any number of keystrokes buffered in the OS, or hardware---your
  37. program cannot know this without talking to special drivers or taking other
  38. such similar platform-specific measures that are outside of the scope of C.
  39.  
  40. >What is the best way to get rid of the '\n' from the input buffer after
  41. >a scanf() statement?  In the same book on page 263 I've seen the following
  42. >format used in an fscanf() format specifier:
  43. >
  44. > %*[\n]
  45. >
  46. >which is explained as follows:
  47. >
  48. >"...and %*[\n] consumes and discards the NEWLINE character at the end of
  49. > an input line."
  50. >
  51. >But I've not had much luck with it when used as follows:
  52. >
  53. >    scanf("%c %*[\n]", &test);
  54.  
  55. That's because your book is wrong. Which one is it?
  56.  
  57.  
  58. You want to match characters that are _not_ equal to a newline:
  59.  
  60.      scanf("%c %*[^\n]", &test);
  61.  
  62.  
  63. The K&R2 book explains this on page 246:
  64.  
  65.     [...] matches the longest non-empty string of input characters
  66.           from the set between brackets.
  67.  
  68. Thus [^\n] means match the longest non-empty string of all input characters
  69. other than a newline.
  70.  
  71. The newline is then consumed because it is whitespace, and scanf() naturally
  72. thrives on vegetation consisting of spaces, tabs and newlines.
  73.  
  74. You and your students ought to be using a solid reference like the Kernighan
  75. and Ritchie, or the Harbison and Steele book. Your department can surely afford
  76. a copy of the ISO/IEC standard as well, for reference purposes, no?
  77.